<!DOCTYPE html>
<html>
<head>
    {% load static %}
    <meta charset="UTF-8">
    <title>更新机构</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/css/oksub.css">
    <script type="text/javascript" src="/static/lib/loading/okLoading.js"></script>
    <script src="/static/lib/layui/layui.js"></script>
    <link type="text/css" rel="stylesheet" href="/static/lib/layui/treeSelect/metroStyle.css">
    <link type="text/css" rel="stylesheet" href="/static/lib/layui/treeSelect/treeSelectStyle.css">
    <script src="/static/js/jquery-1.11.2.min.js"></script>
    <script type="text/javascript" src="/static/js/jquery.ztree.all.js"></script>
    <script type="text/javascript" src="/static/js/treeSelect.2.0.js"></script>
    <script type="text/javascript" src="/static/js/xm-select.js"></script>
    <script type="text/javascript" src="/static/js/xeutils.js"></script>
</head>
<body>
<div class="ok-body">
    <!--form表单-->
    <!--form表单-->
    <form class="layui-form layui-form-pane ok-form">
        {% csrf_token %}
        <div class="layui-form-item">
            <label class="layui-form-label">机构代码<span style="color: red">*</span></label>
            <div class="layui-input-block">
                <input type="text" name="id" id="id" class="layui-input" style="display: none">
                <input type="text" name="key" id="key" placeholder="请输入机构/部门代码，且不重复！" autocomplete="off"
                    class="layui-input" maxlength="18" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">机构名称<span style="color: red">*</span></label>
            <div class="layui-input-block">
                <input type="text" name="name" id="name" placeholder="请输入机构/部门名称" autocomplete="off" class="layui-input"
                    lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属机构</label>
            <div class="layui-input-block" id="parent" name="parent">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">负责人</label>
            <div class="layui-input-block">
            <input type="text" name="owner" id="owner" placeholder="请输入负责人姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-block">
            <input type="text" name="phone" id="phone" placeholder="请输入电话号码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
            <input type="text" name="email" id="email" placeholder="请输入邮箱地址" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
            <input type="text" name="sort" id="sort" lay-verify="number"  placeholder="请输入邮箱地址" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
              <input type="radio"  name="status" value="0" title="启用">
              <input type="radio"  name="status" value="1" title="停用">
            </div>
          </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="description" id="description" placeholder="请输入备注内容/140个汉字" maxlength="255"
                    class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="edit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="close">关闭</button>
            </div>
        </div>
    </form>
</div>
</body>
<!--js逻辑-->

<script type="text/javascript">
    function child(d) {
        var data1 = d;
        layui.use(["element", "form", "laydate", "okLayer", "okUtils"], function () {
            let $ = layui.$;
            let form = layui.form;
            let laydate = layui.laydate;
            let okLayer = layui.okLayer;
            let okUtils = layui.okUtils;
            okLoading.close();
            $('#id').val(data1.id);
            $('#key').val(data1.key);
            $('#name').val(data1.title);
            $('#parent').val(data1.parent);
            $('#owner').val(data1.owner);
            $('#phone').val(data1.phone);
            $('#email').val(data1.email);
            $('#sort').val(data1.sort);
            if(data1.status=='0'){
                $('div.layui-unselect.layui-form-radio')[0].click();
            }else{
                $('div.layui-unselect.layui-form-radio')[1].click();
            }
            $('#description').val(data1.description);
            
            // 回显上级单位
            var initvalue;
            if(data1.parent===null){
                initvalue=0;
            }else{
                initvalue=data1.parent
            }
            var demo3 = xmSelect.render({
                el: '#parent',
                name:'parent_id',
                model: {label: {type: 'text'}},
                radio: true,
                layVerify:'verDeptName',
                layVerType:'message',
                clickClose: true,
                initValue:[initvalue],
                prop:{
	                name:'title',
                    value:'id',
                },
                tree: {
                    show: true,
                    strict: false,
                    expandedKeys: [-1],
                },
                height: 'auto',
                data() {
                    return []
                }
            });
            $.ajax({
                url: "{% url 'system:getDeptDataTree' %}",
                type: "GET",
                dataType:"json",
                success: function (ret) {
                    var node = ret;
                    demo3.update({
                        data: XEUtils.toArrayTree(node.data,{ id:'id',parentKey: 'parent', strict: true, children:'children' }),
                        autoRow: true,
                    })
                },
                error: function () {
                }
            });
            form.verify({
                verDeptName:function(value,item){
                    if(data1.id==value){
                        return '不能选择自己为上级机构!';
                    }
                }
            })
            // 提交检查
            form.on("submit(edit)", function (data) {
                // if (data.field.select == data1.id) {
                // okLayer.redCrossMsg('不能选择自己为上级单位');
                // return false;
                // }
                okUtils.ajax("/system/editDeptData/" + data.field.id, "post", data.field, true).done(function (response) {
                    okLayer.greenTickMsg(response.msg, function () {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                        window.parent.location.reload();
                    });
                }).fail(function (error) {
                    console.log(error)
                });
                return false;
            });
            $("#close").click(function () {
                let index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                parent.layer.close(index);

            });
        })
    };


</script>
</html>
